#define _CRT_SECURE_NO_WARNINGS 1
  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
 
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* cur = head;
        int count = 0;
        while (cur)
        {
            count++;
            cur = cur->next;
        }
        int a = count / k;
        ListNode* newhead = new ListNode(0);
        ListNode* prev = newhead;
        cur = head;
        while (a)
        {
            int d = k;
            ListNode* tmp = cur;
            while (d)
            {
                ListNode* next = cur->next;
                cur->next = prev->next;
                prev->next = cur;
                cur = next;
                d--;
            }
            prev = tmp;
            a--;
        }
        prev->next = cur;
        cur = newhead->next;
        return cur;
    }
};